package com.cinemtl.manager;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.cinemtl.dbConnection.DbConnection;
import com.cinemtl.beans.User;

public class UserManager {

	public static User getById(int id) {
		User user = new User();
		String query = "SELECT * FROM user WHERE id = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setInt(1, id);

			ResultSet result = ps.executeQuery();

			while (result.next()) {
				user.setId(result.getInt("id"));
				user.setUsername(result.getString("username"));
				user.setPassword(result.getString("password"));
				user.setFname(result.getString("fname"));
				user.setLname(result.getString("lname"));
				user.setAdress(result.getString("adress"));
				user.setEmail(result.getString("email"));
				user.setSex(result.getString("sex"));
				user.setBirthday(result.getDate("birthday"));
				user.setTel(result.getString("tel"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();

		return user;
	}

	public static User getByUsername(String username) {
		User user = new User();
		String query = "SELECT * FROM user WHERE username = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, username);

			ResultSet result = ps.executeQuery();

			while (result.next()) {
				user.setId(result.getInt("id"));
				user.setUsername(result.getString("username"));
				user.setPassword(result.getString("password"));
				user.setFname(result.getString("fname"));
				user.setLname(result.getString("lname"));
				user.setAdress(result.getString("adress"));
				user.setEmail(result.getString("email"));
				user.setSex(result.getString("sex"));
				user.setBirthday(result.getDate("birthday"));
				user.setTel(result.getString("tel"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();

		return user;
	}

	public static boolean verifyUser(String username, String password) {
		boolean success = false;
		String query = "SELECT username,password FROM user WHERE username = ? and password = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, username);
			ps.setString(2, password);
			ResultSet result = ps.executeQuery();
			if (result.next()) {
				success = true;

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();
		return success;
	}

	public static void insert(User user) {
		String query = "INSERT INTO user (username,password,fname,lname,adress,email,sex,birthday,tel) "
				+ "values(?,?,?,?,?,?,?,?,?)";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPassword());
			ps.setString(3, user.getFname());
			ps.setString(4, user.getLname());
			ps.setString(5, user.getAdress());
			ps.setString(6, user.getEmail());
			ps.setString(7, user.getSex());
			ps.setDate(8, new java.sql.Date(user.getBirthday().getTime()));
			ps.setString(9, user.getTel());
			ps.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();

	}

	public static void resetPass(String email, String password) {
		String query = "UPDATE user SET password = ? WHERE email = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, password);
			ps.setString(2, email);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();

	}

	public static boolean checkAvailable(String username) {
		boolean success = false;
		String query = "SELECT username,password FROM user WHERE username = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, username);
			ResultSet result = ps.executeQuery();
			if (result.next()) {
				success = true;

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();
		return success;
	}

	public static boolean verifyEmail(String email) {
		boolean success = false;
		String query = "SELECT * FROM user WHERE email = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, email);
			ResultSet result = ps.executeQuery();
			if (result.next()) {
				success = true;

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();
		return success;
	}

	public static void insertUUID(String uuid, String email) {
		String query = "UPDATE user SET reset_uuid = ? WHERE email = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, uuid);
			ps.setString(2, email);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();
	}

	public static boolean checkUUID(String uuid) {
		String query = "SELECT * FROM user WHERE reset_uuid = ?";
		PreparedStatement ps;
		boolean exists = false;
		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, uuid);
			ResultSet result = ps.executeQuery();
			if (result.next()) {
				exists = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();
		return exists;
	}

	public static String getEmailByUUID(String uuid) {
		String query = "SELECT email FROM user WHERE reset_uuid = ?";
		PreparedStatement ps;
		String email = "";
		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, uuid);
			ResultSet result = ps.executeQuery();
			while(result.next()){
				email = result.getString("email");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();
		return email;
	}
	public static void removeUUID(String email){
		String query = "UPDATE user SET reset_uuid = null WHERE email = ?";
		PreparedStatement ps;
		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, email);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();
	}
}
